package com.ehsy.conf;

import org.springframework.context.annotation.Configuration;
import org.springframework.messaging.simp.config.MessageBrokerRegistry;
import org.springframework.web.socket.config.annotation.EnableWebSocketMessageBroker;
import org.springframework.web.socket.config.annotation.StompEndpointRegistry;
import org.springframework.web.socket.config.annotation.WebSocketMessageBrokerConfigurer;

@Configuration
@EnableWebSocketMessageBroker
public class WebSocketConfig implements WebSocketMessageBrokerConfigurer {
    @Override
    public void configureMessageBroker(MessageBrokerRegistry config) {
        // 配置消息代理，其中"/app"用于处理应用级别的消息，"/topic"或"/queue"用于代理目的地
        config.enableSimpleBroker("/topic", "/queue");
        // 设置应用程序的前缀，用于处理客户端发送到服务器的消息
        config.setApplicationDestinationPrefixes("/app");
    }

    @Override
    public void registerStompEndpoints(StompEndpointRegistry registry) {
        // 注册WebSocket的STOMP端点，并允许使用SockJS作为备选方案（应对不支持WebSocket的老旧浏览器）
        registry.addEndpoint("/ws").withSockJS();
    }
}